<?php  
/** 
 * This is a component to send email from CakePHP using PHPMailer 
 * @link http://bakery.cakephp.org/articles/view/94 
 * @see http://bakery.cakephp.org/articles/view/94 
 */ 

class EmailComponent 
{ 

	/*nuevo componente */
	/**
* Send email using SMTP Auth by default.
*

var $from = 'pococlaro@gmail.com'; 
var $fromName = "gatekeeper Pronto Deli"; 
var $sitePrefix = '[MySite]';
//var $useSMTPAuth = false;
var $smtpUserName = 'pococlaro@gmail.com';
var $smtpPassword = 'nadielasabe1986'; 
var $smtpHostNames = "smtp.gmail.com";
var $smtpPort='465';
var $text_body = null;
var $html_body = null;
var $to = null;
var $toName = null;
var $subject = null;
var $cc = null;
var $bcc = null;
var $template = 'email/default';
var $attachments = null;

var $controller;

function startup( & $controller)
{
   $this->controller = & $controller;
}

/**
* Helper function to generate the appropriate template location
*
* @return string CakePHP location of the template file
* @param object $template_type
*
function templateLocation($template_type)
{
   return ('..'.DS.strtolower($this->controller->name).DS.$this->template.$template_type);
}

/**
* Renders the content for either html or text of the email
*
* @return string Rendered content from the associated template
* @param object $type_suffix
*
function bodyContent($type_suffix)
{
   $temp_layout = $this->controller->layout; // store the current controller layout

   if ($type_suffix == 'html')
       $this->controller->layout = '..'.DS.'email';
   else
       $this->controller->layout = '';

   $mail = $this->controller->render($this->templateLocation('_'.strtolower($type_suffix)));
   // render() automatically adds to the controller->output, we'll remove it
   $this->controller->output = str_replace($mail, '', $this->controller->output);

   $this->controller->layout = $temp_layout; // restore the controller layout
   return $mail;
}

function attach($filename, $asfile = '')
{
   if ( empty($this->attachments))
   {
       $this->attachments = array ();
       $this->attachments[0]['filename'] = $filename;
       $this->attachments[0]['asfile'] = $asfile;
   } else
   {
       $count = count($this->attachments);
       $this->attachments[$count+1]['filename'] = $filename;
       $this->attachments[$count+1]['asfile'] = $asfile;
   }
}

function send()
{
   App::import('Vendor', 'PHPMailer', array ('file'=>'phpmailer'.DS.'class.phpmailer.php'));

   $mail = new PHPMailer();

   $mail->IsSMTP();
  // $mail->SMTPAuth = $this->useSMTPAuth;
   $mail->Host = $this->smtpHostNames;
   $mail->Username = $this->smtpUserName;
   $mail->Password = $this->smtpPassword;

   $mail->From = $this->from;
   $mail->FromName = $this->fromName;
   $mail->AddAddress($this->to, $this->toName);
   $mail->AddReplyTo($this->from, $this->fromName);

   $mail->CharSet = 'ISO-8859-1';
   $mail->WordWrap = 80; // set word wrap to 50 characters

   if (! empty($this->attachments))
   {
       foreach ($this->attachments as $attachment)
       {
           if ( empty($attachment['asfile']))
           {
               $mail->AddAttachment($attachment['filename']);
           } else
           {
               $mail->AddAttachment($attachment['filename'], $attachment['asfile']);
           }
       }
   }

   $mail->IsHTML(true); // set email format to HTML

   $mail->Subject = $this->sitePrefix.' '.$this->subject;
   $mail->Body = $this->bodyContent('html');
   $mail->AltBody = $this->bodyContent('text');

   $result = $mail->Send();

   if ($result == false)
       $result = $mail->ErrorInfo;

   return $result;
}*/
	/*fin nuevo componente*/
	
  /** 
   * Send email using SMTP Auth by default. 
   */ 
   
    var $from         = 'pococlaro@gmail.com'; 
    var $fromName     = "gatekeeper Pronto Deli"; 
    var $smtpUserName = 'pococlaro@gmail.com';  // SMTP username 
    var $smtpPassword = 'nadielasabe1986'; // SMTP password 
    var $smtpHostNames= "smtp.gmail.com";  // specify main and backup server 
	var $smtpPort='465';
    var $text_body = null; 
    var $html_body = null; 
    var $to = null; 
    var $toName = null; 
    var $subject = null; 
    var $cc = null; 
    var $bcc = null; 
    var $template = 'email/default'; 
    var $attachments = null; 

    var $controller; 

    function startup( &$controller ) { 
      $this->controller = &$controller; 
    } 

    function bodyText() { 
	 
      ob_start(); 
      $temp_layout = $this->controller->layout; 
      $this->controller->layout = '';  // Turn off the layout wrapping 
      echo $this->controller->render($this->template . '_text');  
      $mail = ob_get_clean(); 
      $this->controller->layout = $temp_layout; // Turn on layout wrapping again 
      return $mail; 
	  
    } 

    function bodyHTML() { 
	
	
      ob_start(); 
      $temp_layout = $this->controller->layout; 
      $this->controller->layout = 'email';  //  HTML wrapper for my html email in /app/views/layouts 
      echo $this->controller->render($this->template . '_html');  
      $mail = ob_get_clean(); 
      $this->controller->layout = $temp_layout; // Turn on layout wrapping again 
      return $mail; 
    } 

    function attach($filename, $asfile = '') { 
      if (empty($this->attachments)) { 
        $this->attachments = array(); 
        $this->attachments[0]['filename'] = $filename; 
        $this->attachments[0]['asfile'] = $asfile; 
      } else { 
        $count = count($this->attachments); 
        $this->attachments[$count+1]['filename'] = $filename; 
        $this->attachments[$count+1]['asfile'] = $asfile; 
      } 
    } 


    function send() 
    { 
    //App::import('Vendor','phpmailer'.DS.'class.phpmailer'); 
	App::import('Vendor', 'PHPMailer', array('file' => 'phpmailer'.DS.'class.phpmailer.php'));

    $mail = new PHPMailer(); 

    $mail->IsSMTP();            // set mailer to use SMTP 
    $mail->SMTPAuth = true;     // turn on SMTP authentication 
    $mail->Host   = $this->smtpHostNames; 
    $mail->Username = $this->smtpUserName; 
    $mail->Password = $this->smtpPassword; 

    $mail->From     = $this->from; 
    $mail->FromName = $this->fromName; 
    $mail->AddAddress($this->to, $this->toName ); 
    $mail->AddReplyTo($this->from, $this->fromName ); 

    $mail->CharSet  = 'UTF-8'; 
    $mail->WordWrap = 50;  // set word wrap to 50 characters 

    if (!empty($this->attachments)) { 
      foreach ($this->attachments as $attachment) { 
        if (empty($attachment['asfile'])) { 
          $mail->AddAttachment($attachment['filename']); 
        } else { 
          $mail->AddAttachment($attachment['filename'], $attachment['asfile']); 
        } 
      } 
    } 

    $mail->IsHTML(true);  // set email format to HTML 

    $mail->Subject = $this->subject; 
    $mail->Body    = $this->bodyHTML(); 
    $mail->AltBody = $this->bodyText(); 

    $result = $mail->Send(); 

    if($result == false ) $result = $mail->ErrorInfo; 

    return $result; 
    } 
	
} 
?>